<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>卡片名称</span>
      <el-button type="primary" @click="dialogFormVisible = true">添加分类</el-button>
    </div>
    <div>
      <!-- 添加类别弹框 -->
      <el-dialog :before-close="cancel" title="添加类别" :visible.sync="dialogFormVisible">
        <el-form :model="ruleFrom" :rules="rules" ref="from">
          <el-form-item label="分类名称" :label-width="formLabelWidth" prop="cate_name">
            <el-input autocomplete="off" v-model="ruleFrom.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" :label-width="formLabelWidth" prop="cate_alias">
            <el-input autocomplete="off" v-model="ruleFrom.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="login">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div>
      <!-- 修改类别弹框 -->
      <el-dialog title="修改类别" :visible.sync="elalogFormVisible">
        <el-form ref="editForm" :model="deitData" :rules="rules">
          <el-form-item label="分类名称" :label-width="formLabelWidth" prop="cate_name">
            <el-input autocomplete="off" v-model="deitData.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" :label-width="formLabelWidth" prop="cate_alias">
            <el-input autocomplete="off" v-model="deitData.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel1">取 消</el-button>
          <el-button type="primary" @click="login1">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 默认页面显示的内容 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="序号" width="100"> </el-table-column>
      <el-table-column prop="cate_name" label="分类名称" width="513"> </el-table-column>
      <el-table-column prop="cate_alias" label="分类别名" width="513"> </el-table-column>
      <el-table-column label="操作" width="513">
        <template slot-scope="scope">
          <el-button @click="del(scope.row.id)">删除</el-button>
          <el-button @click="update(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
// 按需导入所需要的api
import { getCartListAPI, addCartListAPI, delCartListAPI, updateCategoryAPI } from '@/api/cart'
export default {
  components: {},
  data() {
    return {
      // 请求过来的数据
      tableData: [],
      // 编辑的数据
      deitData: {},
      // 添加时的数据
      ruleFrom: {
        cate_alias: '',
        cate_name: ''
      },
      // 控制修改弹框的显示与隐藏
      elalogFormVisible: false,
      // 控制添加弹框的显示与隐藏
      dialogFormVisible: false,
      formLabelWidth: '120px',
      // 添加的验证规则
      //
      rules: {
        cate_name: [
          { required: true, message: '分类名称必须填写', trigger: 'blur' },
          { min: 1, max: 10, message: '分类名称必须是1~10位非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类别名必填', trigger: 'blur' },
          { min: 1, max: 10, message: '分类名称必须是1~10位非空字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 发请求自调用
    this.getCart()
  },
  methods: {
    // 获取文章分类列表
    async getCart() {
      const { data: res } = await getCartListAPI()
      // console.log(res)
      if (res.code === 0) {
        // 请求的结果等于tabledata
        this.tableData = res.data
      }
    },
    // 添加分类功能
    login() {
      // 点击确定让添加弹框隐藏
      this.dialogFormVisible = false
      // console.log(1)
      // 发送请求// 新增文章分类
      this.$refs.from.validate(async vaild => {
        if (!vaild) return
        const { data: res } = await addCartListAPI(this.ruleFrom)
        console.log(res)
        if (res.code === 0) {
          // 成功弹窗
          this.$message.success(res.message)
          // 重置表单
          this.$refs.from.resetFields()
          // 添加完之后调用获取文章分类列表方法 去渲染
          this.getCart()
        } else {
          // 失败弹窗
          this.$message.error(res.message)
        }
      })
    },
    // 添加类点击取消功能
    cancel() {
      // 重置表单
      this.$refs.from.resetFields()
      // 添加弹窗隐藏
      this.dialogFormVisible = false
    },
    // 删除功能
    del(index) {
      this.$confirm('你确定删除吗, 是否继续?', '提示', {
        type: 'warning',
        callback: async r => {
          if (r === 'confirm') {
            // console.log(index)
            const { data: res } = await delCartListAPI(index)
            if (res.code === 0) {
              this.$message.success(res.message)
              this.getCart()
            } else {
              this.$message.error(res.message)
            }
          }
        }
      })
    },
    // 修改功能
    update(val) {
      this.elalogFormVisible = true
      console.log(val)
      this.deitData = { ...val }
    },
    login1() {
      console.log(122)
      this.elalogFormVisible = true
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await updateCategoryAPI(this.deitData)
        if (res.code === 0) {
          this.$message.success(res.message)
          // 调用方法重新渲染页面
          this.getCart()
          // 关闭修改弹框
          this.elalogFormVisible = false
        } else {
          this.$message.error(res.message)
        }
      })
    },
    cancel1() {
      this.elalogFormVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
